//
//  PrefsViewController.m
//  FuelLog
//
//  Created by Pure Mass Communications on 10/18/08.
//  Copyright 2008 . All rights reserved.
//
//  Placed under GPL v2 with the kind permission of Pure Mass Communications
//
//  Modified to make compile under SDK 3.1 by Olaf Greck in May 2010
//
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#import "PrefsViewController.h"
#import "FuelLogAppDelegate.h"
#import "Pref.h"

@implementation PrefsViewController

/*
// override if customizing
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/
 
/*
// Implement loadView to create a view hierarchy programmatically.
- (void)loadView {
}
*/

/* 
// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	mpgButton.selected = NO;
	ukButton.selected = NO;
	kplButton.selected = NO;
	lpkButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = NO;
	odometer0Button.selected = NO;
	odometer1Button.selected = NO;
	fuel2Button.selected = NO;
	fuel3Button.selected = NO;
	cost2Button.selected = NO;
	cost3Button.selected = NO;
	
	Pref *pref = [FuelLogAppDelegate pref];
	units = pref.units;
	switch (units) {
		case ENGLISH:
			mpgButton.selected = YES;
			break;
		case UK:
			ukButton.selected = YES;
			break;
		case METRIC:
			kplButton.selected = YES;
			break;
		case INV_METRIC:
			lpkButton.selected = YES;
			break;
		case NORWAY:
			lp10kButton.selected = YES;
		default:
			dpfButton.selected = YES;
	}
	
	distanceDigits = pref.odometerDigits;
	switch (distanceDigits) {
		case 1:
			odometer1Button.selected = YES;
			break;
		default:
			odometer0Button.selected = YES;
	}
	
	volumeDigits = pref.volumeDigits;
	switch (volumeDigits) {
		case 2:
			fuel2Button.selected = YES;
			break;
		default:
			fuel3Button.selected = YES;
	}
	
	costDigits = pref.costDigits;
	switch (costDigits) {
		case 3:
			cost3Button.selected = YES;
			break;
		default:
			cost2Button.selected = YES;
	}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (void)dealloc {
	[mpgButton dealloc];
	[ukButton dealloc];
	[kplButton dealloc];
	[lpkButton dealloc];
	[lp10kButton dealloc];
	[dpfButton dealloc];
	[odometer0Button dealloc];
	[odometer1Button dealloc];
	[fuel2Button dealloc];
	[fuel3Button dealloc];
	[cost2Button dealloc];
	[cost3Button dealloc];
    [super dealloc];
}

- (IBAction)done:(id)sender {
	Pref *pref = [FuelLogAppDelegate pref];
	[pref setPrefs:distanceDigits volumeDigits:volumeDigits costDigits:costDigits units:units];
	[self dismissModalViewControllerAnimated:YES];
}

- (IBAction)cancel:(id)sender {
	[self dismissModalViewControllerAnimated:YES];
}

- (IBAction)mpgClick:(id)sender {
	ukButton.selected = NO;
	kplButton.selected = NO;
	lpkButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = NO;
	mpgButton.selected = YES;
	units = ENGLISH;
}

- (IBAction)ukClick:(id)sender {
	mpgButton.selected = NO;
	kplButton.selected = NO;
	lpkButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = NO;
	ukButton.selected = YES;
	units = UK;
}

- (IBAction)kplClick:(id)sender {
	mpgButton.selected = NO;
	ukButton.selected = NO;
	lpkButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = NO;
	kplButton.selected = YES;
	units = METRIC;
}

- (IBAction)lpkClick:(id)sender {
	mpgButton.selected = NO;
	ukButton.selected = NO;
	kplButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = NO;
	lpkButton.selected = YES;
	units = INV_METRIC;
}

- (IBAction)lp10kClick:(id)sender {
	mpgButton.selected = NO;
	ukButton.selected = NO;
	kplButton.selected = NO;
	lpkButton.selected = NO;
	dpfButton.selected = NO;
	lp10kButton.selected = YES;
	units = NORWAY;
}

- (IBAction)dpfClick:(id)sender {
	mpgButton.selected = NO;
	ukButton.selected = NO;
	kplButton.selected = NO;
	lpkButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = YES;
	units = GENERIC;
}

- (IBAction)odometer0Click:(id)sender {
	odometer1Button.selected = NO;
	odometer0Button.selected = YES;
	distanceDigits = 0;
}

- (IBAction)odometer1Click:(id)sender {
	odometer0Button.selected = NO;
	odometer1Button.selected = YES;
	distanceDigits = 1;
}

- (IBAction)fuel2Click:(id)sender {
	fuel3Button.selected = NO;
	fuel2Button.selected = YES;
	volumeDigits = 2;
}

- (IBAction)fuel3Click:(id)sender {
	fuel2Button.selected = NO;
	fuel3Button.selected = YES;
	volumeDigits = 3;
}

- (IBAction)cost2Click:(id)sender {
	cost3Button.selected = NO;
	cost2Button.selected = YES;
	costDigits = 2;
}

- (IBAction)cost3Click:(id)sender {
	cost2Button.selected = NO;
	cost3Button.selected = YES;
	costDigits = 3;
}

@end
